<?php
class Location
{
	/* Const */
	private $_table_location				= 'locations';
	private $_table_location_fields		= array('location_id', 'parent_id', 'location_name', 'location_alias', 'ordering', 'active');
	
	/* Static */
	static private $_locations = null;
	static protected $_instance = null;
	
	/**
	 * Get object instance
	 */
	
	public static function getInstance()
	{
		if( null === self::$_instance ) {
			self::$_instance = new self();	
		}
		return self::$_instance;
	}
	
	/**
	 * Get location
	 * @param int $id
	 */
	
	public function getLocation($id, $where = null)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_location, '*')
						->where('location_id = ?', (int)$id);
		if($where != null) {
			$select->where($where);
		}
		return $storage->fetchRow($select);
	}
	
	/**
	 * Get location by alias
	 * @param string $alias
	 */
	
	public function getLocationByAlias($alias, $where = null)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_location, '*')
						->where($storage->quoteInto('location_alias = ?', $alias));
		if($where != null) {
			$select->where($where);
		}
		return $storage->fetchRow($select);
	}
	
	/**
	 * Delete location
	 * @param int | array $id
	 */
	
	public function deleteLocation($id)
	{
		$storage = My_Globals::getStorage();
		$idm = $this->getChildIdOfLocation($id, 0);
		array_push($idm, $id);
		return $storage->delete($this->_table_location, $storage->quoteInto('location_id IN (?)', $idm));
	}
	
	/**
	 * Save location
	 * @param array $data
	 * @param int $id
	 */
	
	public function saveLocation($data)
	{
		$storage = My_Globals::getStorage();
		$data = My_Globals::initData($this->_table_location_fields, $data);
		if(isset($data['location_id']) && $data['location_id'] > 0) {
			// Update
			$rs = $storage->update($this->_table_location, $data, 'location_id = '.$data['location_id']);
		} else {
			// Insert
			$rs = $storage->insert($this->_table_location, $data);
		}
		// Clean
		unset($data);
		// Return
		return $rs;
	}
	
	/**
	 * Get location list
	 * @param int $page
	 * @param int $limit
	 */
	
	public function getLocationList($page = 0, $limit = 0, $order = null, $where = null)
	{
		if(self::$_locations == null) {
			// Set page
			if($page == 0) $page = 1;
			$storage = My_Globals::getStorage();
			$select = $storage->select()
						->from($this->_table_location, '*');
	
			if($where != null) {
				$select->where($where);
			}
			if($limit > 0) {
				$select->limitPage($page, $limit);
			}
			if(is_array($order)) {
				$select->order($order);
			} else {
				$select->order(array('ordering ASC', 'location_name ASC'));
			}
			self::$_locations = $storage->fetchAll($select);
		}
		return self::$_locations;
	}
	
	/**
	 * Check location alias exist
	 * @param string $alias
	 */
	
	public function checkLocationAliasExist($alias)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_location, 'COUNT(*) AS total')
						->where($storage->quoteInto('location_alias = ?', $alias));
		$row = $storage->fetchRow($select);
		if($row['total'] > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * Get child id of location
	 * @param array $cats
	 * @param int $id
	 */
	
	public function getChildIdOfLocation($id, $where = null)
	{
		$idr = array();
		$cats = $this->getLocationList(null, null, null, $where);
		foreach ($cats  as $cat) {
			if($cat['parent_id'] == $id) {
				array_push($idr, $cat['location_id']);
				$idr = array_merge($idr, self::getChildIdOfLocation($cat['location_id'], $where));
			}
		}
		return $idr;		
	}
	
	/**
	 * Get tree location
	 * @param int $id
	 * @param string $where
	 */
	
	public function getTreeLocation($id, $where = null)
	{
		$cats = $this->getLocationList(null, null, null, $where);
		if(sizeof($cats)) {
			$arr = array();			
			while($id != 0) {
				foreach($cats  as $cat) {
					if($cat['location_id'] == $id) {
						array_push($arr, $cat);
						$id = $cat['parent_id'];
						break;
					}
				}
			}
			// Reverse Array
			return array_reverse($arr);
		} else {
			return null;
		}
	}
}